package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabInspectCompanyReqno;
import com.ysd.lis.mapper.lab.LabInspectCompanyReqnoMapper;
import com.ysd.lis.request.lab.MakeReqNoDto;
import com.ysd.lis.service.SysRuleSetService;
import com.ysd.lis.service.lab.LabInspectCompanyReqnoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 送检单位-预制条码 服务实现类
 * </p>
 *
 * @author WH
 * @since 2024-10-23
 */
@Service
public class LabInspectCompanyReqnoServiceImpl extends ServiceImpl<LabInspectCompanyReqnoMapper, LabInspectCompanyReqno> implements LabInspectCompanyReqnoService {

    @Autowired
    private LabInspectCompanyReqnoMapper labInspectCompanyReqnoMapper;

    @Autowired
    public SysRuleSetService sysRuleSetService;

    @Override
    public Result makeReqNoList(MakeReqNoDto makeReqNoDto) {
        List<LabInspectCompanyReqno> list = new ArrayList<>();
        if (Objects.nonNull(makeReqNoDto.getAmount()) && makeReqNoDto.getAmount() > 0) {
            for (int i = 0; i < makeReqNoDto.getAmount(); i++) {
                String reqno = sysRuleSetService.createRuleCodeListByDate(makeReqNoDto.getCompanyRules(), 1, makeReqNoDto.getReqTime()).get(0);
                LabInspectCompanyReqno companyReqno = new LabInspectCompanyReqno();
                companyReqno.setCompanyCode(makeReqNoDto.getCompanyCode());
                companyReqno.setCompanyName(makeReqNoDto.getCompanyName());
                companyReqno.setReqNo(reqno);
                companyReqno.setReqTime(makeReqNoDto.getReqTime());
                companyReqno.setUseState(0);
                list.add(companyReqno);
            }
        }
        boolean b = this.saveBatch(list);

        if (b) {
            return Result.succ(1, "添加成功", list);
        } else {
            return Result.fail(400, "添加失败", "");
        }
    }

    @Override
    public Result deleteReqNo(LabInspectCompanyReqno labInspectCompanyReqno) {
        int i = labInspectCompanyReqnoMapper.deleteById(labInspectCompanyReqno.getId());
        if (i > 0) {
            return Result.succ(1, "删除成功", labInspectCompanyReqno.getId());
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result getReqNoList(MakeReqNoDto makeReqNoDto) {
        LambdaQueryWrapper<LabInspectCompanyReqno> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(makeReqNoDto.getCompanyCode()), LabInspectCompanyReqno::getCompanyCode, makeReqNoDto.getCompanyCode());
        queryWrapper.eq(StringUtils.isNotBlank(makeReqNoDto.getReqNo()), LabInspectCompanyReqno::getReqNo, makeReqNoDto.getReqNo());
        if (StringUtils.isNotBlank(makeReqNoDto.getReqNoStart()) && StringUtils.isNotBlank(makeReqNoDto.getReqNoEnd())) {
            queryWrapper.between(LabInspectCompanyReqno::getReqNo, makeReqNoDto.getReqNoStart(), makeReqNoDto.getReqNoEnd());
        }
        queryWrapper.eq(Objects.nonNull(makeReqNoDto.getUseState()), LabInspectCompanyReqno::getUseState, makeReqNoDto.getUseState());
        if (Objects.nonNull(makeReqNoDto.getStartTime()) && Objects.nonNull(makeReqNoDto.getEndTime())) {
            queryWrapper.between(LabInspectCompanyReqno::getReqTime, makeReqNoDto.getStartTime(), makeReqNoDto.getEndTime());
        }
        queryWrapper.eq(LabInspectCompanyReqno::getDelFlag, 0);
        return Result.succ(labInspectCompanyReqnoMapper.selectList(queryWrapper));
    }
}
